/** *************************************************************************
 *  @file       main.c
 *  @brief	    802.1AS-2020 OSS同步主函数
 *  @date		2022/11/19
 *  @author		yalan.liu
 *  @version	1.0.0
 ****************************************************************************/
#include "./include/8021AS_PTP.h"
#include "./include/ptp_tsninsight.h"
#include "./include/software_read.h"
#include "./include/hardware_report.h"
#include <signal.h>

void handler(int sig)
{
	printf("%s\n", "OSS is over!!!");
	system("killall -9 OSS");

	exit(0);
}

void create_debug_file()
{
	FILE *fp;

	char str[] = "****** debug message ******\n";
	char str1[] = "****** offset message ******\n";
	char str2[] = "****** delay message ******\n";
	char str3[] = "****** sync message ******\n";

	fp = fopen("debug_log.txt", "w");
	fwrite(str, sizeof(char), strlen(str), fp);
	fclose(fp);

	fp = fopen("oss_offset_log.txt", "w");
	fwrite(str1, sizeof(char), strlen(str1), fp);
	fclose(fp);

	fp = fopen("oss_delay_log.txt", "w");
	fwrite(str2, sizeof(char), strlen(str2), fp);
	fclose(fp);

	fp = fopen("oss_sync_log.txt", "w");
	fwrite(str3, sizeof(char), strlen(str3), fp);
	fclose(fp);

	return;
}

int tsninsight_set_run_data(sync_context *context, opensync_reg_data *regisetr_data)
{
	u8 period = 0;
	u16 tmp_mid = 0;
	u32 sync_period = 0;

	// opensync地址空间赋值
	regisetr_data->ctrl_mode = 0;						 // HG项目默认为集中式
	regisetr_data->protocol_type = htonl(1); // HG项目默认为PTP协议类型
	regisetr_data->master_mid = htonl(context->context_info.gm_mid);

	tmp_mid = context->context_info.gm_mid;

	if (tmp_mid > MAX_NODE_NUM)
	{
		sync_period = 0;
	}
	else
	{
		period = context->context_info.node_context[tmp_mid]->cfg_para.sync_period; // 单位是2^n ms
		sync_period = 1 << period;
	}
	regisetr_data->sync_period = htonl(sync_period);

	printf("protocol_type=%d,master_mid=%d,sync_period=%d\n", ntohl(regisetr_data->protocol_type), ntohl(regisetr_data->master_mid), ntohl(regisetr_data->sync_period));

	return 0;
}

void resource_clear(sync_context *sync_contex_data)
{
	tsninsight_destroy();
	tsmp_pkt_send_recv_destroy();
	sync_context_destory(sync_contex_data);

	return;
}

int main(int argc, char *argv[])
{
	int ret = 0;
	char temp_net_interface[16] = {0};
	u32 sw_version = 0x20240205;
	u16 oss_mid = 0;
	sync_context g_sync_context = {0};
	u8 *sync_para_cfg_xml_file = NULL;

	opensync_reg_data run_data = {0};
	sync_state_table state_table[MAX_NODE_NUM] = {0};

	sync_para_cfg_xml_file = SYNC_CFG_PARA_FILE; // 同步配置参数文本
	run_data.ver_num = htonl(sw_version);

	u32 master_mid = 0;

	// 参数个数判断
	if (argc < 2)
	{
		printf("input format:./OSS net_interface\n");
		return 0;
	}
	sprintf(temp_net_interface, "%s", argv[1]);

	// sprintf(temp_net_interface, "%s", "eth0");

	create_debug_file();

	// 时间同步系统初始化
	ret = sync_init(temp_net_interface);
	if (ret == -1)
	{
		printf("sync_init fail\n");
		tsmp_pkt_send_recv_destroy();
		return 0;
	}

#if 1
	// TSNInsight_init通信初始化
	ret = tsninsight_init();
	if (ret == -1)
	{
		printf("tsninsight_init fail\n");
		resource_clear(&g_sync_context);
		return 0;
	}
#endif
	// 同步参数配置
	ret = sync_para_cfg(temp_net_interface, &g_sync_context, sync_para_cfg_xml_file);
	if (ret == -1)
	{
		printf("sync_para_cfg fail\n");
		resource_clear(&g_sync_context);
		return 0;
	}

	tsninsight_set_run_data(&g_sync_context, &run_data);

	master_mid = ntohl(run_data.master_mid);
	state_table[master_mid].node_mid = run_data.master_mid;

	// printf("protocol_type=%d,master_mid=%d,sync_period=%d\n",ntohl(run_data.protocol_type),ntohl(run_data.master_mid),ntohl(run_data.sync_period));

	// 同步状态监控
	ret = sync_state_supervisor(&g_sync_context, &run_data, &state_table[0]);
	if (ret == -1)
	{
		printf("sync_state_supervisor fail\n");
		resource_clear(&g_sync_context);
		return 0;
	}

	// 释放资源
	resource_clear(&g_sync_context);

	return 0;
}